package com.cci.kangdao.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.cci.kangdao.dao.model.ServiceStationT;
import com.cci.kangdao.service.WorkOrderListService;
import com.cci.kangdao.service.impl.SMSEnum;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.cci.kangdao.service.AllServiceStationService;
import com.cci.kangdao.utilTool.ParameterTool;

@Controller
public class AllServiceStationController {

    /**
     * 获取所有服务站
     * 服务经理转派服务站列表
     * APP传入字段
     * PositionLat	故障地点经度
     * PositionLon	故障地点纬度
     * ChannelId	服务渠道id
     * StationName	服务站名称（转派页面查询服务站使用）
     * UserId		用户id
     * page		当前页
     */
    private Logger log = Logger.getLogger(this.getClass().getName());

    @Autowired
    private AllServiceStationService allServiceStationService;

    @Autowired
    private WorkOrderListService workOrderListService;

    @RequestMapping("/getAllServiceStation.aspx")
    public ModelAndView getAllServiceStation(HttpServletRequest request,
                                             HttpServletResponse response) throws Exception {
        try {
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("response", response);
            map.put("request", request);
            return allServiceStationService.getAllServiceStation(map);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);

            String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
            ParameterTool.writeResponse(response, msg);
        }
        return null;
    }

     /**
      * @description: 根据一网信息查询二网服务站
      * @param:
      * @return:
      * @exception:
      * @author: shiliqiang
      * @company: CTY Technology Co.,Ltd
      * @since: 2022/5/18 16:01
      * @version: V1.0
      */
    @RequestMapping(value = "/getChildrenServiceStation.aspx", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public void getChildrenServiceStation(@RequestBody String jsonStr,HttpServletResponse response) throws Exception {
        log.info("接口传参为"+jsonStr);
        JSONObject jsonObj = JSONObject.parseObject(jsonStr);
        if (jsonObj == null) {
            throw new Exception("入参不能为空");
        }
        Long ServiceStationId = jsonObj.getLong("ServiceStationId");
        if (ServiceStationId == null) {
            ParameterTool.writeErrorResponse(response, SMSEnum.type17.getIndex(), "ServiceStationId不能为空");
        }
        Long ChannelId = jsonObj.getLong("ServiceStationId");
        if (ChannelId == null) {
            ParameterTool.writeErrorResponse(response, SMSEnum.type17.getIndex(), "ChannelId不能为空");
        }
        String PositionLat = jsonObj.getString("PositionLat");
        String PositionLon = jsonObj.getString("PositionLon");
        String StationName = jsonObj.getString("StationName");
        ParameterTool.writeResponse(response, allServiceStationService.getChildrenServiceStation(ServiceStationId,StationName, PositionLon, PositionLat, ChannelId).toString());
    }

     /**
      * @description: 根据一网id查询是否存在二网服务站
      * @param:
      * @return:
      * @exception:
      * @author: shiliqiang
      * @company: CTY Technology Co.,Ltd
      * @since: 2022/5/18 16:02
      * @version: V1.0
      */
    @RequestMapping(value = "/queryIsHaveChildrenStation.aspx", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public void queryIsHaveChildrenStation(@RequestBody String jsonStr,HttpServletResponse response) throws Exception {
        log.info("接口传参为"+jsonStr);
        JSONObject jsonObj = JSONObject.parseObject(jsonStr);
        if (jsonObj == null) {
            throw new Exception("入参不能为空");
        }
        Long stationId = jsonObj.getLong("stationId");
        if (stationId == null) {
            ParameterTool.writeErrorResponse(response, SMSEnum.type17.getIndex(), "stationId不能为空");
        }
        List<ServiceStationT>  stationList = workOrderListService.querychildrenStationByFatherStationId(stationId);
        jsonObj.clear();
        if (stationList != null && stationList.size() > 0) {
            jsonObj.put("isHaveChildren", true);
        } else {
            jsonObj.put("isHaveChildren", false);
        }
        ParameterTool.writeSuccessResponse(response,jsonObj);
    }

    //区域工程师获取获取推荐三家服务站+管辖下所有服务站    分页
    @RequestMapping("/getDBUAllServiceStation.aspx")
    public ModelAndView getDBUAllServiceStation(HttpServletRequest request,
                                                HttpServletResponse response) throws Exception {
        try {
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("response", response);
            map.put("request", request);
            return allServiceStationService.getDBUAllServiceStation(map);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);

            String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
            ParameterTool.writeResponse(response, msg);
        }
        return null;
    }

    //区域工程师获取获取   crm 推荐的服务站
    @RequestMapping("/getCrmDBUAllServiceStation.aspx")
    public ModelAndView getCrmDBUAllServiceStation(HttpServletRequest request,
                                                   HttpServletResponse response) throws Exception {
        try {
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("response", response);
            map.put("request", request);
            return allServiceStationService.getCrmDBUAllServiceStation(map);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);

            String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
            ParameterTool.writeResponse(response, msg);
        }
        return null;
    }

    //区域工程师获取获取   本地和crm 推荐的服务站
    @RequestMapping("/getCrmAndLocalStations.aspx")
    public ModelAndView getCrmAndLocalStations(HttpServletRequest request,
                                               HttpServletResponse response) throws Exception {
        try {
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("response", response);
            map.put("request", request);
            return allServiceStationService.getCrmAndLocalStations(map);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);

            String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
            ParameterTool.writeResponse(response, msg);
        }
        return null;
    }

    //XCEC CCEC获取用户在线报修服务站列表
    @RequestMapping("/getXCECAllServiceStation.aspx")
    public ModelAndView getXCECAllServiceStation(HttpServletRequest request,
                                                 HttpServletResponse response) throws Exception {
        try {
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("response", response);
            map.put("request", request);
            return allServiceStationService.getXCECAllServiceStation(map);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);

            String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
            ParameterTool.writeResponse(response, msg);
        }
        return null;
    }

}
